<%@ page language="java" contentType="text/html; charset=UTF-8"
		pageEncoding="UTF-8"
%><%@include file="/WEB-INF/jsp/include/tags.jspf"
%><html>
<head>
<script type="text/javascript" src="${url:jslib('/js/lib/firebug.lite-1.4.0.min.js')}">
{
	/*
	overrideConsole: false,
	startInNewWindow: true,
	startOpened: true,
	enableTrace: true
	*/
}
</script>
<script type="text/javascript" src="${url:jslib('/js/lib/jquery.form-2.8.7.min.js')}"></script>
<script type="text/javascript">
$(document).ready(function() {
	$(".submit", $("form.postForm")).click(function() {
		var $postForm = $("form.postForm");
		var $attachs = $('ul.attachments li', $postForm);
		$attachs.each(function(index, attach){
			var attachIdForm = '<input type="hidden" name="attachmentId" value="'+$(attach).attr("data-attachmentId")+'"/>';
			$postForm.append(attachIdForm);
		});
		$postForm.submit();
	});
	$(".upload", $("form.attachment")).click(function(){
		var $form = $(this).parents("form");
		$form.ajaxForm({
			type:'post',
			dataType:'text',
			iframe:true,
			beforeSubmit: function(arr, $tform, options){
				//console.debug("arr.length="+arr.length);
				return true;
			},
			success: function(data) {
				var json = jQuery.parseJSON(data);
				if(json.error == true) {
					$(".uploadError", $form).html("ERROR="+json.cause);
					return;
				}
				$(".uploadError", $form).html("");
				$postForm = $("form.postForm");
				var $attachments = $("div.attachments ul.attachments", $postForm);
				for(var i=0;i<json.attachments.length;i++) {
					var a = json.attachments[i];
					if(a.image == "true") {
						var li = $("<li/>").attr("data-attachmentId", a.id);
						$("<img/>").attr("src", a.small).appendTo(li);
						li.appendTo($attachments);
					}
				}
			}
		});
		$form.submit();
	});
});
</script>
</head>
<body>

<c:if test="${!empty command}">
	<form:form cssClass="postForm" action="/blog/${username}/post" method="post">
		Title: <form:input path="title" /><br/>
		Contents: <form:textarea path="contents" /><br/>
		<div class="attachments">
			<ul class="attachments">
			</ul>
		</div>
		<input type="button" class="submit" value="Submit" />
	</form:form>

	<form class="attachment" action="/attachments/imageUpload" method="post" enctype="multipart/form-data">
		<input type="hidden" name="category" value="blog"/>
		<input type="file" name="file" /><br/>
		<span class="uploadError"></span>
		<input type="button" class="upload" value="Upload"/>
	</form>
</c:if>

<ul>
<c:forEach var="post" items="${result.list}">
	<li><a href="/blog/${post.user.username}/${post.id}">${post.title}</a><br/>
		Comment:${post.commentCount} ${post.createdDate}
	</li>
</c:forEach>
</ul>
</body>
</html>